package com.lonely.fx.拖拽;

import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.geometry.Bounds;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseDragEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @date: 2022-07-27 23:42
 * @author: ztkj-hzb
 * @description:
 **/
public class 按钮拖拽移动 extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        HBox hBox = new HBox();
        Button button = new Button("Button");
        hBox.getChildren().addAll(button);
        primaryStage.setScene(new Scene(hBox));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();

        //鼠标拖拽，重新设设置按钮位置
        button.setOnMouseDragged(event -> {

            System.out.println(event.getSceneX());
            if(event.getSceneX()<=button.getWidth()/2){
                return;
            }

            button.setTranslateX(event.getSceneX() - button.getWidth() / 2);
            button.setTranslateY(event.getSceneY() - button.getHeight() / 2);



        });

        hBox.setOnMouseDragOver(new EventHandler<MouseDragEvent>() {
            @Override
            public void handle(MouseDragEvent event) {
                //System.out.println("hbox 触发");
            }
        });

    }
}
